from bson import ObjectId
from wtforms import fields, validators, Form, ValidationError

from app.validators import BaseForm, UidForm
from app.errors import ParameterError


class Word(Form):
    word = fields.StringField(validators=[validators.DataRequired()])

    def validate_word(self, field):
        if not (word := field.data):
            raise ParameterError(msg='不能包含空白词语')
        if len(word) > 40:
            raise ParameterError(msg=f'{word}超过40个字符')


class Keywords(BaseForm):
    words = fields.FieldList(Word)


class QueryKeywordForm(UidForm):
    word = fields.StringField(validators=[validators.Length(max=40)])


class QueryKeywordTypeForm(UidForm):
    pass
    # type = fields.IntegerField(validators=[validators.DataRequired(), validators.NumberRange(min=1, max=8)])


def keyword_check(form, field):
    field.data = field.data.strip()
    if not field.data:
        raise ParameterError(msg='不能包含空白词语')
    if len(field.data) > 40:
        word = field.data[:40] + '...'
        raise ParameterError(msg=f'{word} 超过40个字符')


class SaveCustomKeywordsForm(UidForm):
    words = fields.FieldList(fields.StringField(validators=[keyword_check]))
    type = fields.IntegerField(validators=[validators.DataRequired(), validators.NumberRange(min=1, max=8)])

    def validate_words(self, field):
        if (count := len(field.data)) > 300:
            raise ParameterError(msg=f'您最多可以添加300个自定义敏感词, 当前添加{count}个')


class QueryWhiteKeywordForm(UidForm):
    page = fields.IntegerField(default=1)
    per_page = fields.IntegerField(default=10, validators=[validators.NumberRange(min=1, max=500)])
    word = fields.StringField()

    def validate_word(self, field):
        field.data = field.data.strip() if field.data else ''


class SaveWhiteKeywordsForm(UidForm):
    words = fields.FieldList(fields.StringField(validators=[keyword_check]))

    # type = fields.IntegerField(validators=[validators.DataRequired(), validators.NumberRange(min=1, max=8)])

    def validate_words(self, field):
        if (count := len(field.data)) > 500:
            raise ParameterError(msg=f'您最多可以添加500个白名单词, 当前添加{count}个')


def check_oid(form, field):
    # 必须传入id
    if not field.data:
        raise ValidationError('必须传入id')
    try:
        _id = ObjectId(field.data)
    except Exception:
        raise ValidationError('id格式错误.')
    field.data = _id


def check_field(form, field):
    # 必须传入模板名称
    if not field.data:
        raise ValidationError(f'字段不能为空')


class DeleteWhiteKeywordForm(UidForm):
    ids = fields.FieldList(fields.StringField(validators=[check_oid]))


class QueryProfileTemplateForm(UidForm):
    limit = fields.StringField(validators=[validators.Length(max=40)])
    page = fields.IntegerField(default=1)
    name = fields.StringField(validators=[validators.Length(max=100)])


class SaveProfileTemplateForm(UidForm):
    name = fields.StringField(validators=[check_field])
    description = fields.StringField()
    plugin_list = fields.FieldList(fields.StringField())


class UpdateProfileTemplateForm(SaveProfileTemplateForm):
    custom_profile_template_id = fields.StringField()


class DeletedProfileTemplateForm(UidForm):
    custom_profile_template_ids = fields.FieldList(fields.StringField())


class GetProfileTemplateForm(UidForm):
    custom_profile_template_id = fields.StringField(validators=[check_field])
